// search模块的小仓库
import {reqGetBannerList,reqGetCommodityList} from '@/api'
import axios from 'axios'

const state ={
    //轮播图数据
    bannerList:[],
    // 商品信息
    commodityList:[],
    // 数据获取的商品信息
    goodsList:[]
}
const mutations={
    GATBANNERLIST(state,value){
        state.bannerList = value
    },
    GATCOMMODITYLIST(state,value){
        state.commodityList = value
    },
    GATALLGOODS(state,value){
        // console.log("值",value,state.commodityList)
        // for(var i=0;i<value.length;i++){
        //     state.commodityList.push(value[i])
        //     console.log("查的值为",value[i],state.commodityList)
        // }
        state.goodsList = value
    }
}
const actions={
    //获取首页轮播图的数据
    async getBannerList({commit}){
        let result = await reqGetBannerList();
        if(result.code === 200){
            commit("GATBANNERLIST",result.data)
            // console.log(result.data)
        }
    },
    // 获取mock里的商品信息
    async getCommodityList({commit}){
        let result = await reqGetCommodityList();
        // console.log(result)
        if(result.code === 200){
            commit("GATCOMMODITYLIST",result.data)
            // console.log(result.data)
        }
    },
    // 获取数据库里的商品信息
    async getGoodsList({commit}){
        axios.get("http://localhost:8080/api/getAllGoods/getAllGoodslist",{
            params:{
              id:1
            }
          }).then(
            res=>{
              let result = res.data
              console.log("aa",result)
              if(res.status == 200){
                  commit("GATALLGOODS",result)
              }
              console.log("a",res)
  
            },
            error=>{
              console.log("b",error.message)
            }
          )
    },
}
const getters={
    getAllgoods(state){
        // console.log("值",state.goodsList.length,state.commodityList,state.commodityList)
        var allgoods = []
        allgoods = state.commodityList.concat(state.goodsList)
        // console.log("获取的所有商品的值为",allgoods)
        return allgoods
    }
}

export default{
    state,
    mutations,
    actions,
    getters
}